package Top_Interview_Questions.Bit_Manipulation;

/**
 * @Author: 吕庆龙
 * @Date: 2020/2/1 20:22
 * <p>
 * 功能描述:
 */
public class _0190 {
    public static void main(String[] args) {
        _0190 test = new _0190();
        String s = "11111111111111111111111111111101";

        test.reverseBits2(43261596);
    }

    /**
     * https://leetcode-cn.com/problems/reverse-bits/solution/
     * xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-4-9/
     *
     * 这个是解法一,解法二有时间再看。
     */
    public int reverseBits2(int n) {
        int res = 0;
        int count = 0;
        while (count < 32) {
            res <<= 1;  //res 左移一位空出位置
            res |= (n & 1); //得到的最低位加过来
            n >>= 1;//原数字右移一位去掉已经处理过的最低位
            count++;
        }
        return res;
    }


    /**
     * 输入: 00000010100101000001111010011100
     * 输出: 00111001011110000010100101000000
     *
     * 这个方法他说参数有问题
     */
    public int reverseBits1(int n) {
        StringBuffer s = new StringBuffer();
        int flag = 1;
        for (int i = 0; i < 32; i++) {
            if ((n & flag) == 1){
               s.append("1");
            }else{
               s.append("0");
            }
            n = n >> 1;
        }
        System.out.println(s);
        return Integer.valueOf(s.toString(),2);
    }
}
